package com.ht.ui;

import com.ht.aems.swing.util.SwingUtil;
import com.ht.aems.ucp.AvcDevKeys;
import devmodel.MapObject;
import org.javabuilders.swing.SwingJavaBuilder;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Wangjiaxiang
 * Date: 12-4-18
 */
public class DynamicInfoDialog extends JDialog{
    private DynamicInfoPanel dynamicPanel;
    private JPanel centerPanel;
    private MapObject mapObject;
    private java.util.List<MapObject> needAddStationSubDev1 = new ArrayList<MapObject>();
    private java.util.List<MapObject> needAddStationSubDev2 = new ArrayList<MapObject>();

    public DynamicInfoDialog(Frame owner, String title, boolean modal, DynamicInfoPanel dynamicInfoPanel) {
        super(owner, title, modal);
        needAddStationSubDev1.clear();
        needAddStationSubDev2.clear();
        SwingJavaBuilder.build(this);
        this.dynamicPanel = dynamicInfoPanel;
        centerPanel.setLayout(new BorderLayout());
        centerPanel.add((JPanel) dynamicPanel);
        pack();
        SwingUtil.centerWindow(this);
        setVisible(true);
    }

    public MapObject getMapObject() {
        return mapObject;
    }

    public List<MapObject> getNeedAddStationSubDev1() {
        return needAddStationSubDev1;
    }

    public List<MapObject> getNeedAddStationSubDev2() {
        return needAddStationSubDev2;
    }

    private void okAction() {
        String result = dynamicPanel.checkUiInfo();
        if(result != null) {
            JOptionPane.showMessageDialog(this, result);
        }else {
           mapObject = dynamicPanel.fillUiInfo(mapObject);
           if(dynamicPanel instanceof SubstationInfoPanel) {
               needAddStationSubDev1.addAll(((SubstationInfoPanel)dynamicPanel).getNeedAddBuseAndTransformer());
               needAddStationSubDev2 .addAll(((SubstationInfoPanel)dynamicPanel).getNeedAddCompensator());
           }
           cancelAction();
        }
    }

    private void cancelAction() {
        dynamicPanel.clear();
        dispose();
    }

}
